import simplejson
import time
import flags
import os

class log():


    def __init__(self):
        self.check_path(flags.DATA_PATH)
        self.check_path(flags.DATA_LOG_PATH)
        self.check_path(flags.DATA_TAB_PATH)

    def save(self,data):
        t = self.time_ymd()
        fn = flags.DATA_LOG_PATH + "triplog" + t + ".json"
        c = simplejson.dumps(data)
        try:
            f = open(fn,'a')
            f.write(c+'\n')
            f.close()
        except:
            self.check_path(flags.DATA_PATH)
            self.check_path(flags.DATA_LOG_PATH)            
            f = open(fn,'a')
            f.write(c+'\n')
            f.close()

    def time_ymd(self):
        #return time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())
        return time.strftime("%Y%m%d", time.localtime())

    def check_path(self,path):
        try:
            os.mkdir(path)
        except:
            pass
